package com.nbjtjc.safe.service.edu;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduCertificateIssueDeptMapper;
import com.nbjtjc.safe.model.edu.EduCertificateIssueDept;

@Service
public class EduCertificateIssueDeptService {
	@Autowired
	EduCertificateIssueDeptMapper eduCertificateIssueDeptMapper;

	public Result add(EduCertificateIssueDept eduCertificateIssueDept){
		Result result = new Result();
		
		if(ObjectUtil.isNull(eduCertificateIssueDept)||ObjectUtil.isNull(eduCertificateIssueDept.getDeptName())){
			result.setFalse("发证单位名称不能为空");
			return result;
		}
		
		int res=0;
		//id为空新增，不为空更新
		if(ObjectUtil.isNull(eduCertificateIssueDept.getId())){
			
			EduCertificateIssueDept certificateIssueDept = new EduCertificateIssueDept();
			certificateIssueDept.setDeptName(eduCertificateIssueDept.getDeptName());
			if(CollectionUtil.isNotEmpty(eduCertificateIssueDeptMapper.findByName(certificateIssueDept))){
				result.setFalse("已有该发证单位，不能重复");
				return result;
			}
			
			eduCertificateIssueDept.setCancel(0);			
			res=eduCertificateIssueDeptMapper.insertSelective(eduCertificateIssueDept);
			if(res==0){
				result.setFalse("新增发证单位失败");
				return result;
			}
		}else{
			res=eduCertificateIssueDeptMapper.updateByPrimaryKeySelective(eduCertificateIssueDept);
			if(res==0){
				result.setFalse("更新发证单位失败");
				return result;
			}
		}
		result.setData(eduCertificateIssueDept);

		return result;
	}
	
	public Result find(EduCertificateIssueDept eduCertificateIssueDept){
		Result result = new Result();
	    PageHelper.startPage(eduCertificateIssueDept.getPageCurrent(), eduCertificateIssueDept.getPageSize());
	    eduCertificateIssueDept.setCancel(0);
		List<EduCertificateIssueDept> eduCertificateIssueDepts = eduCertificateIssueDeptMapper.search(eduCertificateIssueDept);
		result.setData(new PageInfo<>(eduCertificateIssueDepts));
			
		return result;
	}
	
	public Result delete(Integer certDeptId){
		Result result = new Result();
		if(ObjectUtil.isNull(certDeptId)){
			result.setFalse("删除ID不能为空");
			return result;
		}
		EduCertificateIssueDept eduCertificateIssueDept = eduCertificateIssueDeptMapper.selectByPrimaryKey(certDeptId);
		if(ObjectUtil.isNull(eduCertificateIssueDept)){
			result.setFalse("该ID无符合的发证单位");
			return result;
		}else{
			eduCertificateIssueDept.setCancel(1);
			int res = eduCertificateIssueDeptMapper.updateByPrimaryKeySelective(eduCertificateIssueDept);
			if(res == 0){
				result.setFalse("作废失败");
				return result;
			}
		}
		
		return result;
	}
}
